*****************************************************************
*** replication code for Wei and Zhao (2022) with pseudo data ***
*** *************************************************************

*** Load data ***
use "./sample10000x_pseudo.dta", clear

quietly{
*** sample selection between originated/denied, purchase/refinance loans ***
*** denied: denied applications; refi: refinance applications
gen origpur = !denied&!refi
gen origrefi = !denied&refi
gen denypur = denied&!refi
gen denyrefi = denied&refi

*** Loan Purchaser type ****
* 1,2,3,4,5,6,7 <-> (dummy1:GSE), dummy2 (PLSP), dummy3, ..., dummy7
tab purchaser_type2, generate(purchaser_type2_dummy) 
}

*** Table 2 ***

foreach x in origpur origrefi denypur denyrefi {

display("`x'")
tabstat app2act if `x', by(applicant_race_whole) statistics (mean sd p25 p50 p75 count)


}





*** Table 3 ***

* Panel A *
tab purchaser_type2 if origpur&year<=2003 
tab purchaser_type2 if origpur&year>2003

* Panel B *
* 2001-2003 *
tab applicant_race_whole purchaser_type2 if origpur&year<=2003, column w
* 2004-2006 *
tab applicant_race_whole purchaser_type2 if origpur&year>2003, column w

* Panel C *
* 2001-2003 *
tab applicant_race_whole purchaser_type2 if origpur&year<=2003, summarize(app2act) means w
* 2004-2006 *
tab applicant_race_whole purchaser_type2 if origpur&year>2003, summarize(app2act) means w

* Panel D *
* 2001-2003 *
tab applicant_race_whole purchaser_type2 if origpur&year<=2003, summarize(app2act) standard w
* 2004-2006 *
tab applicant_race_whole purchaser_type2 if origpur&year>2003, summarize(app2act) standard w


*** Table 4 ***
* Panel A *
tabstat app2act fico_orig cltv deliq_10to15_ots if merged, by(applicant_race_whole) statistics(mean sd)

* Panel B and C *
foreach x in FIXED HYBRID IOflag BALLOON lowdoc fulldoc fastOTD slowOTD {
	display("`x'")
	tab applicant_race_whole if merged, summarize(app2act) means standard	
}

* Panel D *
foreach x in FIXED HYBRID IOflag BALLOON lowdoc fulldoc fastOTD slowOTD {
	display("`x'")
	tab applicant_race_whole if merged	
}




quietly{
* the month when action taken on application  	
gen actmth=mofd(mdy(action_mon,action_day,year))
format actmth %tm
* county by month fixed effects
egen fips_mth = group(fips_code actmth)
* county by year fixed effects
egen fips_year = group(fips_code year)
* number of applications by month
bys actmth: gen napp = _N
* number of refinance applications by month
bys actmth: egen nrefi = total(refi)

gen log_napp = log(napp)
gen log_nrefi = log(nrefi)


**** regressionion *****
gen preapproval_dummy = preapproval==1
gen log_amount = log(loan_amount)
gen log_income = log(applicant_income)
* stardardization of loan controls *
foreach x in ln_HPA average_wage unemploymentrate_scale  log_amount log_income log_napp log_nrefi fico_orig cltv dti_ratio_back init_rate margin_rate {
      egen `x'_s =std(`x')
}
* replace log_income_s = 0 if misincome 

replace log_napp = log_napp_s
replace log_nrefi = log_nrefi_s

* Year fixed effects
tabulate year, generate(year_dummy)
* Gender fixed effects
tab applicant_sex, generate(applicant_sex_dummy)
* Racial groups
tab applicant_race_whole, generate(applicant_race_dummy)

gen black = applicant_race_dummy2
gen hispanic = applicant_race_dummy3
gen asian = applicant_race_dummy4

gen log_nrefi_black = log_nrefi*black
gen log_nrefi_hispanic = log_nrefi*hispanic
gen log_nrefi_asian = log_nrefi*asian

gen log_napp_black = log_napp*black
gen log_napp_hispanic = log_napp*hispanic
gen log_napp_asian = log_napp*asian

local loanctrl =  "highcost rate_spread ln_HPA_s average_wage_s unemploymentrate_scale_s log_amount_s log_income_s coapplicant preapproval_dummy jumbo applicant_sex_dummy2 applicant_sex_dummy3 applicant_sex_dummy4"



**** regressions with CHMDA data: lender FEs by lender_id_num *****

reghdfe app2act `loanctrl' purchaser_type2_dummy2-purchaser_type2_dummy7 black hispanic asian if origpur & year<=2003, absorb(fips_mth) vce(cluster actmth lender_id_num)
eststo m03

reghdfe app2act `loanctrl' purchaser_type2_dummy2-purchaser_type2_dummy7 black hispanic asian if origpur & year<=2003, absorb(lender_id_num fips_mth) vce(cluster actmth lender_id_num)
eststo m13

reghdfe app2act `loanctrl' purchaser_type2_dummy2-purchaser_type2_dummy7 black hispanic asian if origpur & year>2003, absorb(fips_mth) vce(cluster actmth lender_id_num)
eststo m04

reghdfe app2act `loanctrl' purchaser_type2_dummy2-purchaser_type2_dummy7 black hispanic asian if origpur & year>2003, absorb(lender_id_num fips_mth) vce(cluster actmth lender_id_num)

eststo m14

forvalues ii=1/7 {

if `ii'==2 continue

reghdfe app2act `loanctrl'  black hispanic asian if origpur & year<=2003 &purchaser_type2==`ii', absorb(fips_mth) vce(cluster actmth lender_id_num)
eststo m03_p`ii'

reghdfe app2act `loanctrl'  black hispanic asian if origpur & year<=2003 &purchaser_type2==`ii', absorb(lender_id_num fips_mth) vce(cluster actmth lender_id_num)
eststo m13_p`ii'


}






forvalues ii=1/7 {

reghdfe app2act `loanctrl'  black hispanic asian if origpur & year>2003 &purchaser_type2==`ii', absorb(fips_mth) vce(cluster actmth lender_id_num)
eststo m04_p`ii'

reghdfe app2act `loanctrl'  black hispanic asian if origpur & year>2003 &purchaser_type2==`ii', absorb(lender_id_num fips_mth) vce(cluster actmth lender_id_num)
eststo m14_p`ii'


}

foreach x in origpur origrefi {

reghdfe app2act log_napp log_napp_black log_napp_hispanic log_napp_asian `loanctrl' purchaser_type2_dummy2-purchaser_type2_dummy7 black hispanic asian if `x' , absorb(lender_id_num fips_code action_mon) vce(cluster lender_id_num actmth)
eststo m0_`x'

reghdfe app2act log_nrefi log_nrefi_black log_nrefi_hispanic log_nrefi_asian `loanctrl' purchaser_type2_dummy2-purchaser_type2_dummy7 black hispanic asian if `x' , absorb(lender_id_num fips_code action_mon) vce(cluster lender_id_num actmth)
eststo m1_`x'

}
}



/*** Table 5 ***/
* upper panel for Whole Sample 2001-03&2004-06, GSE 2001-03&2004-06, PLSP 2004-06 *
* m0: specification (a); m1: specification (b) *
estout m03 m13 m04 m14 m03_p1 m13_p1 m04_p1 m14_p1  m04_p2 m14_p2, keep(black hispanic asian) nodiscrete cells(b(star fmt(%6.0g)) se(par))  stats(r2 N, fmt(%6.3f %11.0g) labels(R2 N )) style(fixed) starl(* 0.1 ** 0.05 *** 0.01)

* lower panel for Banks 2001-03&2004-06, Affiliate 2001-03&2004-06, MC 2004-06*
* m0: specification (a); m1: specification (b) *
estout m03_p3 m13_p3 m04_p3 m14_p3 m03_p4 m13_p4 m04_p4 m14_p4  m04_p5 m14_p5, keep(black hispanic asian) nodiscrete cells(b(star fmt(%6.0g)) se(par))  stats(r2 N, fmt(%6.3f %11.0g) labels(R2 N )) style(fixed) starl(* 0.1 ** 0.05 *** 0.01)

/*** Table 6 ***/
* Purchase sample: origpur; Refinance sample: origrefi *
* m0: specification (a); m1: specification (b) *
estout  m0_origpur m1_origpur m0_origrefi m1_origrefi, drop(`loanctrl' purchaser_type2_dummy*) nodiscrete cells(b(star fmt(%6.0g)) se(par))  stats(r2 N, fmt(%6.3f %11.0g) labels(R2 N )) style(fixed) starl(* 0.1 ** 0.05 *** 0.01)

quietly{

* Regressions with merged CHMDA-CoreLogic Data; merged(merge flag for successful merge) 	
local loanctrl2 =  "year_dummy* highcost rate_spread ln_HPA_s average_wage_s unemploymentrate_scale_s log_amount_s log_income_s lowdoc fico_orig_s cltv_s dti_ratio_back_s misdti init_rate_s margin_rate_s FIXED HYBRID IOflag BALLOON pp_pen_flg coapplicant preapproval_dummy jumbo applicant_sex_dummy2 applicant_sex_dummy3 applicant_sex_dummy4"

reghdfe app2act `loanctrl2' black hispanic asian if merged, absorb(lender_id_num fips_code) vce(cluster fips_year)
eststo model0

reghdfe deliq_10to15_ots app2act `loanctrl2' black hispanic asian if merged, absorb(lender_id_num fips_code) vce(cluster fips_year)
eststo model1


foreach x in FIXED HYBRID IOflag BALLOON fastOTD slowOTD lowdoc fulldoc {

reghdfe app2act `loanctrl2' black hispanic asian if `x'&merged, absorb(lender_id_num fips_code) vce(cluster fips_year)
eststo model0_`x'

reghdfe deliq_10to15_ots app2act `loanctrl2' black hispanic asian if `x'&merged, absorb(lender_id_num fips_code) vce(cluster fips_year)
eststo model1_`x'

}
}
/*** Table 7 ***/
/* upper panel for Processing time */
estout model0 model0_FIXED model0_IOflag model0_BALLOON model0_fastOTD model0_slowOTD model0_lowdoc model0_fulldoc, keep(black hispanic asian) nodiscrete cells(b(star fmt(3)) se(par fmt(3)))  stats(r2 N, fmt(%6.3f %11.0g) labels(R2 N )) style(fixed) starl(* 0.1 ** 0.05 *** 0.01)

/* lower panel for Delinquency */
estout model1 model1_FIXED model1_IOflag model1_BALLOON model1_fastOTD model1_slowOTD model1_lowdoc model1_fulldoc, keep(black hispanic asian) nodiscrete cells(b(star fmt(3)) se(par fmt(3)))  stats(r2 N, fmt(%6.3f %11.0g) labels(R2 N )) style(fixed) starl(* 0.1 ** 0.05 *** 0.01)


